package com.zking;

import java.util.*;
import java.util.stream.Collectors;

class Student {
    private String name;
    private int age;
    private String gender;

    private String grade;

    public Student(String name, int age, String gender, String grade) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", grade='" + grade + '\'' +
                '}';
    }
}
public class StreamDemo6 {
    public static void main(String[] args) {
        List<Student> students = Arrays.asList(
                new Student("小明1", 11, "1", "6"),
                new Student("小明2", 12, "2", "5"),
                new Student("小明3", 13, "3", "4"),
                new Student("小明4", 14, "4", "3"),
                new Student("小明5", 15, "5", "2"),
                new Student("小明6", 16, "6", "1"),
                new Student("小明6", 16, "6", "1")
        );

        //得到所有学生年龄列表 s-s.getAge()--> 不会多生成一个类似lambda$0这样的函数
        //Collectors.toList()收集器 收集所有的对象
        List<Integer> collect = students.stream().map(Student::getAge).collect(Collectors.toList());
        System.out.println("所有学生年龄："+collect);
        //Collectors.toSet() 去重
        Set<Integer> collect1 = students.stream().map(Student::getAge).collect(Collectors.toSet());
        System.out.println("所有学生年龄："+collect1);

        //统计汇总信息 Collectors.summarizingInt 有个问题 14出现数据精度丢失
        IntSummaryStatistics collect2 = students.stream().collect(Collectors.summarizingInt(Student::getAge));
        System.out.println("年龄汇总信息："+collect2);
        //分块
        Map<Boolean, List<Student>> collect3 = students.stream().collect(Collectors.partitioningBy(
                s -> s.getGender() == "3"));
        System.out.println(collect3);

        //分组
        Map<String, List<Student>> collect4 = students.stream().collect(Collectors.groupingBy(Student::getGrade));
        System.out.println(collect4);

        //得到所有班级学生的个数
        Map<String, Long> collect5 = students.stream().collect(Collectors.groupingBy(Student::getGrade, Collectors.counting()));
        System.out.println(collect5);
    }
}
